//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.BusinessLogic.Model;
using NopSolutions.NopCommerce.Common;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.BusinessLogic.Events;

namespace NopSolutions.NopCommerce.Web.Modules
{
    public partial class AddressEdit : BaseNopFrontendUserControl
    {
        private bool _isBillingAddress = false;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            Load += new EventHandler(AddressEdit_Load);

            ddlLegalForm.SelectedIndexChanged += new EventHandler(LegalFormSelectedIndexChanged);
            ddlCountry.SelectedIndexChanged += new EventHandler(CountrySelectedIndexChanged);
            ddlStateProvince.SelectedIndexChanged += StateProvinceSelectedIndexChanged;
        }

        void AddressEdit_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (IsNew)
                {
                    lblShippingAddressId.Text = string.Empty;
                    txtFirstName.Text = string.Empty;
                    txtLastName.Text = string.Empty;
                    txtPhoneNumber.Text = string.Empty;
                    txtEmail.Text = string.Empty;
                    txtFaxNumber.Text = string.Empty;
                    txtMobilePhoneNumber.Text = string.Empty;
                    txtAddress.Text = string.Empty;

                    txtCompany.Text = string.Empty;
                    txtCompanyFiscalCode.Text = string.Empty;
                    txtCompanyRegComNumber.Text = string.Empty;
                    txtCompanyBank.Text = string.Empty;
                    txtCompanyBankAccount.Text = string.Empty;

                    txtCity.Text = string.Empty;
                    txtZipPostalCode.Text = string.Empty;
                    if (IsBillingAddress)
                        this.FillCountryDropDownsForBilling();
                    else
                        this.FillCountryDropDownsForShipping();
                    this.FillStateProvinceDropDowns();
                }

                if (Address.CompanyLegalForm)
                {
                    pnlCompany.Visible = true;
                }
                else
                {
                    pnlCompany.Visible = false;
                }
            }
        }

        #region Properties

        public Address Address
        {
            get
            {
                var address = new Address();
                if (!String.IsNullOrEmpty(lblShippingAddressId.Text))
                    address.AddressId = int.Parse(lblShippingAddressId.Text);
                address.FirstName = txtFirstName.Text.Trim();
                address.LastName = txtLastName.Text.Trim();
                address.PhoneNumber = txtPhoneNumber.Text.Trim();
                address.FaxNumber = txtFaxNumber.Text.Trim();
                address.MobilePhoneNumber = txtMobilePhoneNumber.Text.Trim();
                address.Email = txtEmail.Text.Trim();
                address.FullAddress = txtAddress.Text.Trim();
                address.City = txtCity.Text.Trim();

                address.CompanyLegalForm = ddlLegalForm.SelectedValue.Equals("0") ? false : true;
                address.Company = txtCompany.Text.Trim();
                address.CompanyFiscalCode = txtCompanyFiscalCode.Text.Trim();
                address.CompanyRegComNumber = txtCompanyRegComNumber.Text.Trim();
                address.CompanyBank = txtCompanyBank.Text.Trim();
                address.CompanyBankAccount = txtCompanyBankAccount.Text.Trim();

                if (this.ddlCountry.SelectedItem == null)
                    throw new NopException("Countries are not populated");
                address.CountryId = int.Parse(this.ddlCountry.SelectedItem.Value);

                if (this.ddlStateProvince.SelectedItem == null)
                    throw new NopException("State/Provinces are not populated");
                var stateProvince = this.StateProvinceService.GetStateProvinceById(int.Parse(this.ddlStateProvince.SelectedItem.Value));
                if (stateProvince != null && stateProvince.CountryId == address.CountryId)
                    address.StateProvinceId = stateProvince.StateProvinceId;

                address.ZipPostalCode = txtZipPostalCode.Text.Trim();
                return address;
            }
            set
            {
                Address address = value;
                if (address != null)
                {
                    this.lblShippingAddressId.Text = address.AddressId.ToString();
                    this.txtFirstName.Text = address.FirstName;
                    this.txtLastName.Text = address.LastName;
                    this.txtPhoneNumber.Text = address.PhoneNumber;
                    this.txtFaxNumber.Text = address.FaxNumber;
                    this.txtMobilePhoneNumber.Text = address.MobilePhoneNumber;
                    this.txtEmail.Text = address.Email;
                    this.txtAddress.Text = address.FullAddress;
                    this.txtCity.Text = address.City;

                    this.ddlLegalForm.SelectedValue = !address.CompanyLegalForm ? "0" : "1";
                    this.txtCompany.Text = address.Company;
                    this.txtCompanyFiscalCode.Text = address.CompanyFiscalCode;
                    this.txtCompanyRegComNumber.Text = address.CompanyRegComNumber;
                    this.txtCompanyBank.Text = address.CompanyBank;
                    this.txtCompanyBankAccount.Text = address.CompanyBankAccount;

                    if (address.IsBillingAddress)
                        this.FillCountryDropDownsForBilling();
                    else
                        this.FillCountryDropDownsForShipping();
                    CommonHelper.SelectListItem(this.ddlCountry, address.CountryId);

                    FillStateProvinceDropDowns();
                    CommonHelper.SelectListItem(this.ddlStateProvince, address.StateProvinceId);

                    this.txtZipPostalCode.Text = address.ZipPostalCode;
                }
            }
        }

        [DefaultValue(true)]
        public bool IsNew
        {
            get
            {
                object obj2 = this.ViewState["IsNew"];
                return ((obj2 != null) && ((bool)obj2));

            }
            set
            {
                this.ViewState["IsNew"] = value;
            }
        }

        [DefaultValue(true)]
        public bool IsBillingAddress
        {
            get { return _isBillingAddress; }
            set { _isBillingAddress = value; }
        }

        public string ValidationGroup
        {
            get { return txtFirstName.ValidationGroup; }
            set
            {
                txtFirstName.ValidationGroup = txtLastName.ValidationGroup =
                txtEmail.ValidationGroup =
                txtPhoneNumber.ValidationGroup =
                txtCompany.ValidationGroup = txtCompanyRegComNumber.ValidationGroup = txtCompanyFiscalCode.ValidationGroup = txtCompanyBank.ValidationGroup = txtCompanyBankAccount.ValidationGroup =
                txtAddress.ValidationGroup = txtCity.ValidationGroup = txtZipPostalCode.ValidationGroup = value;
            }
        }

        #endregion

        #region Methods

        private void FillCountryDropDownsForShipping()
        {
            this.ddlCountry.Items.Clear();
            var countryCollection = this.CountryService.GetAllCountriesForShipping();
            foreach (var country in countryCollection)
            {
                var ddlCountryItem2 = new ListItem(country.Name, country.CountryId.ToString());
                this.ddlCountry.Items.Add(ddlCountryItem2);
            }
        }

        private void FillCountryDropDownsForBilling()
        {
            this.ddlCountry.Items.Clear();
            var countryCollection = this.CountryService.GetAllCountriesForBilling();
            foreach (var country in countryCollection)
            {
                var ddlCountryItem2 = new ListItem(country.Name, country.CountryId.ToString());
                this.ddlCountry.Items.Add(ddlCountryItem2);
            }
        }

        private void FillStateProvinceDropDowns()
        {
            this.ddlStateProvince.Items.Clear();
            int countryId = 0;
            if (this.ddlCountry.SelectedItem != null)
                countryId = int.Parse(this.ddlCountry.SelectedItem.Value);

            var stateProvinceCollection = this.StateProvinceService.GetStateProvincesByCountryId(countryId);
            foreach (var stateProvince in stateProvinceCollection)
            {
                var ddlStateProviceItem2 = new ListItem(stateProvince.Name, stateProvince.StateProvinceId.ToString());
                this.ddlStateProvince.Items.Add(ddlStateProviceItem2);
            }
            if (stateProvinceCollection.Count == 0)
            {
                var ddlStateProvinceItem = new ListItem(GetLocaleResourceString("Address.StateProvinceNonUS"), "0");
                this.ddlStateProvince.Items.Add(ddlStateProvinceItem);
            }
        }

        #endregion

        #region Events

        public event EventHandler<AddressEventArgs> AddressChanged;

        protected void CountrySelectedIndexChanged(object sender, EventArgs e)
        {
            FillStateProvinceDropDowns();
        }

        void StateProvinceSelectedIndexChanged(object sender, EventArgs e)
        {
            if (AddressChanged != null)
            {
                var args = new AddressEventArgs { Address = Address };
                AddressChanged(this, args);
            }
        }

        void LegalFormSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlLegalForm.SelectedValue == "0")
            {
                pnlCompany.Visible = false;
            }
            else
            {
                pnlCompany.Visible = true;
            }
        }

        #endregion
    }
}